/**
 * Created by Mark_ren on 2017/5/22.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');
var nodemailer = require('nodemailer');
var literatureController = {

    /**
     *  危重症时间 急危e频道接口
     * @param req
     * @param res
     */
    getJWliteratureList: function (req, res) {
        co(function () {

            var type = parseInt(req.query.type);
            var title = "";
            if (req.query.title != undefined && req.query.title != null) {
                title = req.query.title;
            }
            if (req.query.type == 0 || req.query.type == "0") {
                type = "";
            } else {
                type = " and  l.type =" + type;
            }
            seq.sequelize.query("select  l.id,l.title,l.abstract,l.copyright,l.score,l.products,l.create_time ,count(c.id) as cCount" +
                " from lm_literature as l  left join lm_literature_application_log as c on c.title_id = l.ID " +
                "where 1=1" + type + " and l.title like '%" + title + "%'" + " and l.item = " + parseInt(req.query.item) + " and l.sort = " + parseInt(req.query.sort) +
                " group by l.ID order by l.create_time DESC").spread(function (result) {
                    res.json({ "success": true, "result": result });
                });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },

    /**
     *  脑血管时间 卒中e频道接口
     * @param req
     * @param res
     */
    getZZlisteratureList: function (req, res) {
        var type = parseInt(req.query.type);
        var title = "";
        co(function () {
            var title = "";
            if (req.query.title != undefined && req.query.title != null) {
                title = req.query.title;
            }
            if (req.query.type == 0 || req.query.type == "0") {
                type = "";
            } else {
                type = " and  l.type =" + type;
            }
            seq.sequelize.query("select  l.id,l.title,l.abstract,l.copyright,l.score,l.products,l.create_time ,count(c.id) as cCount" +
                " from lm_literature as l  left join lm_literature_application_log as c on c.title_id = l.ID " +
                "where 1=1 " + type + " and l.title like '%" + title + "%'" + " and l.item = " + parseInt(req.query.item) + " and l.sort = " + parseInt(req.query.sort) +
                " group by l.ID order by l.create_time DESC").spread(function (result) {
                    res.json({ "success": true, "result": result });
                });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    /**
     * 发送邮箱
     */
    sendEmail: function (req, res) {
        if (req.query.email == undefined || req.query.email == "" || req.query.memberId == undefined || req.query.memberId == "" || req.query.name == undefined || req.query.name == "" || req.query.tel == undefined || req.query.tel == "" || req.query.hospital == undefined || req.query.hospital == "" || req.query.office == undefined || req.query.office == "" || req.query.product == undefined || req.query.product == "" || req.query.description == undefined || req.query.description == "" || req.query.docType == undefined || req.query.docType == "") {
            res.json({ 'success': false, 'msg': "有信息为空请检查！" });
        } else {
            var name = decodeURI(req.query.name);
            var email = req.query.email;
            var memberId = req.query.memberId;
            var hospital = decodeURI(req.query.hospital);
            var tel = req.query.tel;
            var titleId = req.query.titleId;
            var office = decodeURI(req.query.office);
            var product = decodeURI(req.query.product);
            var description = decodeURI(req.query.description);
            var otherTel = req.query.otherTel;
            var docType = parseInt(req.query.docType);
            var respresentName = decodeURI(req.query.respresentName);
            var docName = "";
            if (req.query.otherTel == undefined || req.query.otherTel == "") {
                otherTel = "未填写";
            }
            if (docType == 1) {
                docName = "文献查找";
            } else if (docType == 2) {
                docName = "文献速递";
            } else {
                docName = "无标题";
            }
            if (titleId == undefined) {
                req.query.titleId = "";
            }
            var respresentHtml = "";
            if (respresentName != undefined && respresentName != "" && respresentName != null) {
                respresentHtml = "是";
            } else {
                respresentHtml = "否";
            }
            var html = "<b>姓名：" + name + "</b></br><b>是否为目标客户：" + respresentHtml + "</b></br><b>手机号：" + tel + "</b></br><b>邮箱：" + email + "</b></br><b>其他联系方式：" + otherTel + "</b></br><b>医院：" + hospital + "</b></br><b>科室：" + office + "</b></br><b>产品：" + product + "</b></br><b>描述：" + description + "</b>";
            req.query.id = util.getUUID();
            req.query.createTime = util.getNowTime();

            seq.memberModule.count({ where: { id: { $ne: memberId }, email: email } }).then(function (memCount) {
                if (memCount == 0) {
                    var transporter = nodemailer.createTransport({
                        //https://github.com/andris9/nodemailer-wellknown#supported-services 支持列表
                        service: 'QQ',
                        host: "smtp.qq.com",
                        port: 465, // SMTP 端口
                        secureConnection: true, // 使用 SSL
                        auth: {
                            user: '3293907884@qq.com',
                            //这里密码不是qq密码，是你设置的smtp密码  sviaojucubufcgii smiyxqordfnudbce
                            pass: 'efslhwkzojnccjhb'
                        }
                    });
                    // NB! No need to recreate the transporter object. You can use
                    // the same transporter object for all e-mails

                    // setup e-mail data with unicode symbols minnali@techpool.com.cn
                    var mailOptions = {
                        from: '3293907884@qq.com', // 发件地址
                        to: 'miq-tp@techpool.com.cn', // 收件列表
                        cc: 'ashe.cao@norlink.cn',//抄送 -
                        subject: docName, // 标题
                        //text和html两者只支持一种
                        //text: '测试', // 标题
                        html: html // html 内容
                    };
                    // send mail with defined transport object info.response
                    transporter.sendMail(mailOptions, function (error, info) {
                        if (error) {
                            res.json({ 'success': false, 'msg': error });
                            return console.log(error);
                        } else {
                            seq.literatureApplicationLogModule.create(req.query).then(function (result) {
                                seq.memberModule.update({ email: email }, { where: { id: memberId } }).then(function () {
                                    res.json({ 'success': true, 'msg': '发送成功！', 'result': result });
                                })
                            })
                        }
                    });
                } else {
                    res.json({ 'success': false, 'msg': "该邮箱已注册！" });
                }
            }).catch(function (e) {
                res.json({ 'success': false, 'msg': '查询邮箱数失败' });
                console.log(e);
            })

        }

    }
}
module.exports = literatureController;